<template>
  <view>
    <view class="scroll-view-container">
      <scroll-view class="left-scroll-view" scroll-y :style="{height:wh + 'px'}">
        <block v-for="(item,index) in cateList" :key="index">
          <view @click="activeChanged(index)" :class="['left-scroll-view-item',active==index ? 'active' :'']">{{item.cat_name}}</view>
        </block>
      </scroll-view>
      <scroll-view class="right-scroll-view" scroll-y :style="{height:wh + 'px'}">
        <block v-for="(item,index) in cateList[currentIndex].children" :key="index">
          <view style="margin: 10px 0; text-align: center; ">/{{item.cat_name}}/</view>
          <!-- <view>{{item.cat_name}}</view> -->
          <view style="display: flex; flex-wrap: wrap;">
            <view  v-for="(item1,index) in item.children" :key="index" >
              <image :src="item1.cat_icon" style="width: 140rpx;height: 150rpx;margin: 0 10px;"></image>
              <view style="text-align: center;font-size: 12px;">{{item1.cat_name}}</view>
            </view>
          </view> 
        </block>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 获取分类列表数据
        cateList: [],
        /* 获取窗口可用的高度*/
        // 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
        wh: 0,
        // 当前活跃项
        active: 0,
        currentIndex: 0
      };
    },
    onLoad() {
      // 获取当前系统的信息
      const sysInfo = uni.getSystemInfoSync();
      // 为 wh 窗口可用高度动态赋值
      this.wh = sysInfo.windowHeight
      this.getCateList()
    },
    methods: {
      // 获取分类的列表数据
      async getCateList() {
        const {
          data
        } = await uni.$http.get('/api/public/v1/categories')
        if (data.meta.status !== 200) return uni.$showMsg()
        // console.log(data.message,999);
        this.cateList = data.message
      },
      activeChanged(i) {
        this.active = i
        this.currentIndex = i
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scroll-view {
      width: 120px;

      .left-scroll-view-item {
        line-height: 60px;
        background-color: #f7f7f7;
        text-align: center;
        font-size: 12px;

        // 激活项的样式
        &.active {
          background-color: #ffffff;
          position: relative;

          // 渲染激活项左侧的红色指示边线
          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #c00000;
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
          }
        }
      }
    }
  }
</style>
